package com.gdyx.app.web.svc.impl;

import com.gdyx.app.web.dao.MpsDeptUserDao;
import com.gdyx.app.web.dao.MpsUserRoleDao;
import com.gdyx.app.web.model.MpsUserRole;
import com.gdyx.app.web.svc.MpsUserRoleSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
@Service
public class MpsUserRoleSvcImpl implements MpsUserRoleSvc {

    @Autowired
    MpsUserRoleDao mpsUserRoleDao;
    @Autowired
    MpsDeptUserDao mpsDeptUserDao;

    @Override
    public void save(MpsUserRole o) {
        mpsUserRoleDao.save(o);
    }

    @Override
    public void saveOrUpdate(MpsUserRole o) {
        mpsUserRoleDao.saveOrUpdate(o);
    }

    @Override
    public void delete(String id) {
        mpsUserRoleDao.delete(id);
    }

    @Override
    public void update(MpsUserRole o) {
        mpsUserRoleDao.update(o);
    }

    @Override
    public int count(Map<String, Object> map) {
        return mpsUserRoleDao.count(map);
    }

    @Override
    public List<MpsUserRole> findUserRoles(Map<String, Object> map) {
        return mpsUserRoleDao.findUserRoles(map);
    }

    @Override
    public List<MpsUserRole> findUserRoleByUid(String id) {
        return mpsUserRoleDao.findUserRoleByUid(id);
    }

    @Override
    public List<MpsUserRole> findUserRoleByRid(String id) {
        return mpsUserRoleDao.findUserRoleByRid(id);
    }

    @Override
    public void deleteByUid(String id) {
        mpsUserRoleDao.deleteByUid(id);
    }

    @Override
    public void deleteByRid(String id) {
        mpsUserRoleDao.deleteByRid(id);
    }

    @Override
    @Transactional
    public void setUserRole(String ids, String roleId, String Ownerid) {
        if(StringUtils.isEmpty(ids)){
            return;
        }
        mpsUserRoleDao.deleteByRid(roleId);
        String[] userIds = ids.split(",");
        //添加
        for (String userId : userIds) {
            //create
            MpsUserRole MpsUserRole=new MpsUserRole();
            MpsUserRole.setId(UUID.randomUUID().toString().toUpperCase().trim());
            MpsUserRole.setUserId(userId);
            MpsUserRole.setRoleId(roleId);
            MpsUserRole.setCreateOwner(Ownerid);
            MpsUserRole.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
            mpsUserRoleDao.save(MpsUserRole);
        }
    }



}
